package com.qf.controller;

import com.qf.entity.Privilege;
import com.qf.service.LoginService;
import com.sun.deploy.net.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/login/")
public class LoginController {
    @Autowired
    private LoginService service;

    //关于用户登录的表单提交
    @RequestMapping( "login.do")
    public String login(String userId, String password, int roleId, HttpSession session) {

        if (password.equals(service.getPasswordByUserId(userId).getPassword())&&roleId==service.getPasswordByUserId(userId).getRoleId()) {

            session.setAttribute("userId",userId);
            session.setAttribute("password",password);
            session.setAttribute("roleId", roleId);
            return "/menu.html";
        } else {
            return "/login.html";
        }

    }
    @RequestMapping( "login1.do")
    @ResponseBody
    public List<Privilege> login1( HttpSession session) {
            int roleId = (int) session.getAttribute("roleId");

            List<Privilege> list = service.findPrivileges(roleId);
            return list;

    }

    //关于修改密码的表单提交
    @RequestMapping(value = "/modifyPassword.do", method = RequestMethod.POST)
    public String modifyPassword(@RequestParam("userId") String userId,
                                 @RequestParam("password") String password,
                                 @RequestParam("newpassword") String newpassword, ModelMap model) {

        if (password.equals(service.getPasswordByUserId(userId))) {
            service.modifyPasswordByUserId(userId, newpassword);
            model.addAttribute("msg", "修改密码成功！");
        } else {
            model.addAttribute("msg", "密码错误！");
        }

        return "index";
    }

}
